/*
 * serverLock.h
 *
 *  Created on: Jun 29, 2011
 *      Author: peckb1
 */

#ifndef SERVERLOCK_H_
#define SERVERLOCK_H_

#include "mailAddress.h"
#include "list.h"

#include <stdio.h>
#include <string.h>

enum ServerLockState
{
    FREE_STATE,
    BUSY_STATE
};

class ServerLock {
    private:
        char* lockName;
        int owningMachine;
        List *lockWaitQueue;
        MailAddress* currentLockOwner;
        enum ServerLockState currentLockState;
        bool isDeleted;
        bool isScheduledForDeletion;

    public:
        ServerLock(char* name, int machineOwner);
        ~ServerLock();

        // 0 for failed
        // 1 for success and send response
        // 2 for success, but on the wait queue
        int Acquire(MailAddress* address);         // these are the only 'operations' on a lock

        // NULL of failure or no new person to alert
        // != null && equal to next person to own the lock
        MailAddress* Release(MailAddress* address); // they are both *atomic*

        bool isHeldByCurrentUser(MailAddress* person);

        void DestroyLock(MailAddress* person);

        bool IsDeleted();

        bool IsScheduledForDeletion();

        char* GetName();
};

#endif /* SERVERLOCK_H_ */
